<template>
	<view class="login">
		<view class="title">
			<view class="login_text " @click="toLogin">
				登录

			</view>
			<view class="register_text active">
				注册
				<text class="active"></text>
			</view>
		</view>
		<view class="login_box">
			<view class="username">
				<text>用户名</text>
				<input type="text" placeholder="请输入用户名" v-model="username" />
			</view>
			<view class="username">
				<text>手机号</text>
				<input type="text" placeholder="请输入手机号" v-model="phone" />
			</view>
			<!-- <view class="username">
				<text>验证码</text>
				<view class="codebox">
					<input type="text" placeholder="请输入验证码" />
					<button class="code" @click="SendCode2" v-show="is_send">获取验证码</button>
					<button class="code" v-show="!is_send">{{time}}s</button>
				</view>
			</view> -->
			<view class="password">
				<text>密码</text>
				<input type="password" placeholder="请输入登录密码" v-model="password" />
			</view>
			<view class="password">
				<text>确认密码</text>
				<input type="password" placeholder="再次输入密码" v-model="password2" />
			</view>
			<!-- <view class="password">
				<text>支付密码</text>
				<input type="password" placeholder="请输入密码" v-model="pay_password" />
			</view> -->
			<view class="password">
				<text>国家</text>
				<uni-data-select v-model="nation" :localdata="range"></uni-data-select>
			</view>
			<view class="password">
				<text>邀请码</text>
				<input type="text" placeholder="请输入邀请码" v-model="code" />
			</view>
			<view class="tips">
				<text></text>
				<navigator url="/pages/login/index" class="forget">
					返回登录
				</navigator>
			</view>
			<button class="btn" @click="register" :loading="loading" :disabled="loading">
				注册
			</button>
			<view class="wxlogin">
				—— <image src="/static/index/wx.png" mode="widthFix"></image> ——
			</view>

		</view>
	</view>
</template>

<script>
	import {
		sendCode,
		register,
		commonParam
	} from "@/api/api.js"
	export default {
		data() {
			return {
				time: 60,
				timer: '',
				phone: '',
				code: '',
				is_send: true,
				username: '',
				password: '',
				password2: '',
				pay_password: '',
				code: '',
				loading: false,
				nation: '',
				range: []
			}
		},
		onLoad() {
			this.commonParam()
		},
		methods: {
			commonParam() {
				var that = this
				commonParam().then(res => {
					if (res.code == 0) {
						let arr = res.data.nation
						arr.forEach(item => {
							that.range.push({
								text: item.name,
								value: item.id
							})
						})
					}
				})
			},
			register() {
				if (this.username == '') {
					return uni.$showMsg('请输入用户名')
				}
				if (this.phone == '') {
					return uni.$showMsg('请输入手机号')
				}
				if (this.password == '') {
					return uni.$showMsg('请输入密码')
				}
				if (this.password != this.password2) {
					return uni.$showMsg('两次密码不一致')
				}
				if (this.nation == '') {
					return uni.$showMsg('请选择国家')
				}

				var that = this
				that.loading = true
				register({
					username: this.username,
					openid: this.openid,
					phone: this.phone,
					password: this.password,
					recommend: this.code,
					password_re: this.password2,
					nation: this.nation
				}).then(res => {
					that.loading = false
					if (res.code == 0) {
						setTimeout(() => {
							uni.setStorageSync('tokens', res.data.token)
							uni.switchTab({
								url: '/pages/index/index'
							})
						})
					} else {
						uni.$showMsg(res.msg)
					}
				})
			},
			getPhone() {
				if (this.phone == '') {
					uni.showToast({
						title: '请输入手机号',
						icon: "none"
					})
					return false
				}
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phone))) {
					uni.showToast({
						title: '请填写正确手机号码',
						icon: "none"
					});
					return false;
				}
				return true
			},
			SendCode2() {
				if (!this.getPhone()) {
					return false
				}
				this.is_send = false
				this.time = 60
				sendCode({
					phone: this.phone
				}).then(res => {
					uni.$showMsg(res.msg)
					if (res.code == 0) {
						this.timer = setInterval(() => {
							this.time--;
							if (this.time < 0) {
								clearInterval(this.timer)
								this.is_send = true
							}
						}, 1000)
					}
				})
			},
			toLogin() {
				uni.redirectTo({
					url: '/pages/login/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	.login {
		padding-top: 20px;
		font-size: 16px;
		min-height: calc(100vh - 44px);
		height: 100%;
		background: $uni-bg-color;
		padding-bottom: 1px;
		box-sizing: border-box;

		.wxlogin {
			text-align: center;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #ccc;

			image {
				width: 30px;
				margin: 0 10px;
			}
		}

		.title {
			display: flex;
			justify-content: space-around;
			align-items: center;
			color: #fff;
			width: 50%;
			margin: 0 auto;

			view {
				display: flex;
				flex-direction: column;
				align-items: center;
			}

			text {
				display: block;
				width: 30px;
				height: 3px;
				background: #fff;
				margin-top: 3px;
			}
		}

		.active {
			font-size: 24px;
		}

		.login_box {
			background: #fff;
			width: 80%;
			padding: 5%;
			padding-top: 10%;
			border-radius: 8px;
			margin: 20px auto;

			text {
				margin: 10px 0;
				display: block;
				font-weight: bold;
				font-size: 14px;
			}

			input {
				border: 1px solid #377751;
				outline: 0;
				border-radius: 5px;
				height: 40px;
				padding-left: 15px;
				margin-top: 10px;
				box-sizing: border-box;
				font-size: 14px;
			}

			.codebox {
				position: relative;

				input {
					padding-right: 120px;
				}

				.code {
					position: absolute;
					right: 10px;
					top: 50%;
					transform: translate(0, -50%);
					font-size: 12px;
					width: 90px;
					background: $uni-bg-color;
					color: #fff;
				}
			}
		}

		.tips {
			display: flex;
			justify-content: space-between;
			font-size: 14px;
			align-items: center;
			margin-top: 10px;

			text {
				font-weight: normal;
				color: #777;
			}

			.forget {
				color: $uni-text-color;
			}
		}


	}
</style>